﻿@model List<SW.Lite.Model.Master.MenuModel>
@using SW.Lite.Core.Extensions
@{
    Layout = "~/Areas/Master/Views/Shared/_Layout.cshtml";
}
@section Header{
    <script type="text/javascript">
        function add() {
            $("#addDialog").dialog({
                title: '添加菜单',
                modal: true,
                width: 600,
                buttons: [
                    {
                        text: "确定",
                        click: function () {
                            Utils.ajax({
                                url: '@Url.Action("Add")',
                                action: 'ADD',
                                data: { 'ParentID': $('#ParentID').val(), 'Icon': $('#Icon').val(), 'Code': $('#Code').val(), 'Title': $('#Title').val(), 'ControllerName': $('#ControllerName').val(), 'ActionName': $('#ActionName').val() },
                                type: 'POST',
                                success: function (d) { if (d.Result) location.reload(); },
                                error: function (status) { }
                            });
                        }
                    }, {
                        text: '取消',
                        click: function () {
                            $(this).dialog("close");
                        }
                    }
                ]
            });
        }

        function deleteAll() {
            var menuIDs = [];
            $('.cboItem:checked').each(function (i, n) {
                menuIDs.push($(n).val());
            });
            if (menuIDs.length < 0) {
                Utils.alert('消息', '请选择要删除的菜单！');
                return;
            }

            Utils.confirm('请确认', '确定删除选定的菜单吗？', function () {
                Utils.ajax({
                    url: '@Url.Action("Delete")',
                    action: 'ADD',
                    data: { 'ids': menuIDs.join(',') },
                    type: 'POST',
                    success: function (d) {
                        if (d.Result) {
                            Utils.alert('消息', '已删除', function () {
                                location.reload();
                            });
                        }
                    },
                    error: function (status) { }
                });
            });
        }

        function selectIcon(icon, src) {
            $('#MenuIcon').removeAttr('class');
            $('#MenuIcon').addClass('i-24');
            $('#MenuIcon').addClass('i-' + $(icon).attr('key'));
            $('#Icon').val('i-' + $(icon).attr('key'));
        }

        function edit() {
            var menuIDs = [];
            $('.cboItem:checked').each(function (i, n) {
                menuIDs.push($(n).val());
            });
            if (menuIDs.length != 1) {
                Utils.alert('消息', '请选择要修改的菜单，且只能选择一项！');
                return;
            }

            Utils.ajax({
                url: '@Url.Action("Single")',
                data: { 'id': menuIDs[0] },
                type: 'POST',
                success: function (menu) {
                    Utils.setObject2Dom(menu);
                    $('#MenuIcon').removeAttr('class');
                    $('#MenuIcon').addClass('i-24');
                    $('#MenuIcon').addClass(menu.Icon);
                    $('#MenuID').val(menu.Id);
                    $('#Code').attr('readonly', 'readonly');
                    $('#Code').addClass('disabled');
                    $("#addDialog").dialog({
                        title: '编辑菜单',
                        modal: true,
                        width: 600,
                        buttons: [
                            {
                                text: "确定",
                                click: function () {
                                    Utils.ajax({
                                        url: '@Url.Action("Edit")',
                                        data: { 'ID': $('#MenuID').val(), 'ParentID': $('#ParentID').val(), 'Icon': $('#Icon').val(), 'Code': $('#Code').val(), 'Title': $('#Title').val(), 'ControllerName': $('#ControllerName').val(), 'ActionName': $('#ActionName').val() },
                                        type: 'POST',
                                        success: function (d) { if (d.Result) location.reload(); },
                                        error: function (status) { }
                                    });
                                }
                            }, {
                                text: '取消',
                                click: function () {
                                    $(this).dialog("close");
                                }
                            }
                        ]
                    });
                },
                error: function (status) { }
            });
        }

        function childrenMenu() {
            var menuIDs = [];
            $('.cboItem:checked').each(function (i, n) {
                menuIDs.push($(n).val());
            });
            if (menuIDs.length != 1) {
                Utils.alert('消息', '请选择要查看子菜单的菜单，且只能选择一项！');
                return;
            }

            location.href = '@Url.Action("Index")?ParentID=' + menuIDs[0];
        }

        function buttons() {
            var menuIDs = [];
            $('.cboItem:checked').each(function (i, n) {
                menuIDs.push($(n).val());
            });
            if (menuIDs.length != 1) {
                Utils.alert('消息', '请选择要查看按钮的菜单，且只能选择一项！');
                return;
            }

            location.href = '@Url.Action("Button")?ID=' + menuIDs[0];
        }

        function save() {
            var menuIDs = [];
            $('.sort').each(function (i, n) {
                var id = $(n).attr('id');
                menuIDs.push('{"Key":"' + id.substring(5, id.length) + '", "Value":' + $(n).val() + '}');
            });
            if (menuIDs.length <= 0) {
                Utils.alert('消息', '无菜单');
                return;
            }

            Utils.ajax({
                url: '@Url.Action("Save")',
                data: { 'sorts': '[' + menuIDs.join(',') + ']' },
                type: 'POST',
                success: function (d) {
                    if (d.Result)
                        location.reload();
                    else
                        Utils.alert('消息', d.Message);
                },
                error: function (status) { }
            });
        }

        function returnParent() {
            location.href = '@Url.Action("Index")';
        }
    </script>
    <style type="text/css">
        .icons { height: 200px; overflow: auto; }
            .icons ul { list-style: none; }
                #MenuIcon, .icons ul li { background-color: #323232; width: 24px; height: 24px; margin: 3px; padding: 4px; -ms-border-radius: 4px; border-radius: 4px; }
                .icons ul li { float: left; }
                    .icons ul li img { cursor: pointer; }
        #MenuIcon { width: 24px; height: 24px; display: inline-block; }
        #MenuIcon, .table .i-24 { padding: 4px; background-color: #323232; background-position: center center; width: 24px; height: 24px; line-height: 24px; -ms-border-radius: 4px; border-radius: 4px; display: inline-block; vertical-align: middle; }
    </style>
}
<div class="panel grid_8">
    <div class="panel-header">
        <span class="i-24 i-user"><a href="@Url.Action("Index")">菜单管理</a></span>
        @if (ViewBag.MenuQueue.Count > 0)
        {
            foreach (var menu in ViewBag.MenuQueue)
            {
                <span class="arrow"></span><span class="i-24 menu i-@menu.Icon"><a href="@Url.Action("Index")?ParentID=@menu.Id">【@menu.Title】</a></span>
            }
        }
    </div>
    <div class="panel-body">
        <div class="panel-toolbar top clearfix">
            <ul>
                <li><a class="ic-16 ic-add" onclick="add();">添加</a></li>
                <li><a class="ic-16 ic-edit" onclick="edit();">编辑</a></li>
                <li><a class="ic-16 ic-disk" onclick="save();">保存</a></li>
                <li><a class="ic-16 ic-cross" onclick="deleteAll();">删除</a></li>
                <li><a class="ic-16 ic-chart-organisation" onclick="childrenMenu();">子菜单</a></li>
                <li><a class="ic-16 ic-bricks" onclick="buttons();">按钮</a></li>
            </ul>
        </div>
        <div class="dataTables_wrapper">
            <table class="datatable-fn table">
                <thead>
                    <tr>
                        <th style="width: 30px;">
                            <input type="checkbox" onchange="Utils.checkAll(this.checked);" />
                        </th>
                        <th style="width: 80px;">菜单图标</th>
                        <th style="width: 200px;">菜单编码</th>
                        <th style="width: auto;">菜单名称</th>
                        <th style="width: 200px;">控制器</th>
                        <th style="width: 200px;">动作</th>
                        <th style="width: 50px;">排序号</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var item in Model)
                    {
                        <tr>
                            <td class="center">
                                <input class="cboItem" type="checkbox" value="@item.Id" />
                            </td>
                            <td class="center">
                                <span class="i-24 @item.Icon"></span>
                            </td>
                            <td class="center">@item.Code</td>
                            <td class="center">@item.Title</td>
                            <td class="center">@item.ControllerName</td>
                            <td class="center">@item.ActionName</td>
                            <td class="center">@Html.TextBox("Sort_" + item.Id, item.Sort, new { @class = "large textinput sort center" })</td>
                        </tr>
                    }
                </tbody>
            </table>
            <div class="dataTables_paginate paging_full_numbers">
                <span>
                    @Html.Pager(20, (int)ViewBag.RecordCount)
                </span>
            </div>
        </div>
    </div>
</div>
<div id="addDialog" class="hide">
    <div class="form-inline form">
        <div class="form-row">
            <label>菜单图标</label><span class="i-24" id="MenuIcon"></span><input id="Icon" type="hidden" /><input id="MenuID" type="hidden" /><input id="ParentID" type="hidden" value="@ViewBag.ParentID" />
            <div class="icons">
                <ul>
                    @foreach (var file in ViewBag.Icons)
                    {
                        <li>
                            <img src="~/Areas/Master/Common/icons/24/@file.Value" key="@file.Key" onclick="selectIcon(this, '@file.Key');" />
                        </li>
                    }
                </ul>
            </div>
        </div>
        <div class="form-row">
            <label>菜单编码</label>
            <div class="form-item large">
                <input id="Code" class="textinput" type="text" />
            </div>
        </div>
        <div class="form-row">
            <label>菜单名称</label>
            <div class="form-item large">
                <input id="Title" class="textinput" type="text" />
            </div>
        </div>
        <div class="form-row">
            <label>控制器名称</label>
            <div class="form-item large">
                <input id="ControllerName" class="textinput" type="text" />
            </div>
        </div>
        <div class="form-row">
            <label>动作名称</label>
            <div class="form-item large">
                <input id="ActionName" class="textinput" type="text" />
            </div>
        </div>
    </div>
</div>
